<template>
  <div class="login">
    <div class="main">
      <!--      <img src="@/assets/logo/logo.png" alt="" class="img1">-->
      <img src="@/assets/images/img_bg.png" alt="" class="img2">
      <h3>数字化档案管理系统</h3>
      <h4>DIGITAL FILE MANAGEMENT SYSTEM</h4>
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
        <h2 class="title">欢迎登录</h2>
        <div v-if="loginType === 'qr'" id="qrCodeLogin" @click="getLoginQr">
          <div class="qr-login">
            <img class="qr-img" src="@/assets/images/qrCode.svg">
          </div>
          <span style="color: #333;padding-top:50px;margin: 10px">
            点击扫码登录
          </span>
        </div>
        <div v-else id="pwdLogin">
          <el-form-item prop="username">
            <el-input v-model="loginForm.username" auto-complete="off" placeholder="用户名" type="text"
                      @blur="isUsername=false" @focus="isUsername=true">
              <svg-icon slot="prefix" class=" input-icon " icon-class="user"
                        v-bind:class="{'color-class':isUsername}"/>
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
                v-model="loginForm.password"
                auto-complete="off"
                placeholder="密码"
                type="password"
                @blur="isPassword=false"
                @focus="isPassword=true"
                @keyup.enter.native="handleLogin"
            >
              <svg-icon slot="prefix" class="el-input__icon input-icon " icon-class="password"
                        v-bind:class="{'color-class':isPassword}"/>
            </el-input>
          </el-form-item>
          <el-form-item prop="code">
            <el-input
                v-model="loginForm.code"
                auto-complete="off"
                placeholder="验证码"
                @blur="isValidCode=false"
                @focus="isValidCode=true"
                @keyup.enter.native="handleLogin"
            >
              <svg-icon slot="prefix" class="el-input__icon input-icon " icon-class="validCode"
                        v-bind:class="{'color-class':isValidCode}"/>
            </el-input>
            <div class="login-code">
              <img :src="codeUrl" @click="getCode"/>
            </div>
          </el-form-item>
          <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
          <el-form-item style="width:100%;">
            <el-button
                :loading="loading"
                size="medium"
                style="width:100%;"
                type="primary"
                @click.native.prevent="handleLogin"
            >
              <span v-if="!loading">登 录</span>
              <span v-else>登 录 中...</span>
            </el-button>
          </el-form-item>
        </div>
        <div class="change-login-type">
          <i v-if="loginType === 'qr'" class="el-icon-unlock change-icon change-to-pwd-i" @click="loginType='pwd'"/>
          <img v-else class=" change-icon change-to-qr-img" src="@/assets/images/qrCode.svg" @click="loginType='qr'"/>
        </div>
      </el-form>
    </div>

    <!--  底部  -->
    <div class="el-login-footer">
      <!--      <span>© 2020 All Rights Reserved. ruoyichina.com<br> 蒙ICP备2020003903号 <img src="" alt=""></span>-->
    </div>
  </div>
</template>

<script>
import {getCodeImg} from "@/api/login";
import Cookies from "js-cookie";
import {decrypt, encrypt} from '@/utils/jsencrypt'
import {getConfigKey} from "@/api/system/config";

export default {
  name: "Login",
  data() {
    return {
      isUsername: false,
      isPassword: false,
      isValidCode: false,
      loginQr: "",
      codeUrl: "",
      cookiePassword: "",
      loginType: "qr",
      loginForm: {
        username: "",
        password: "",
        rememberMe: false,
        code: "",
        uuid: ""
      },
      loginRules: {
        username: [
          {required: true, trigger: "blur", message: "用户名不能为空"}
        ],
        password: [
          {required: true, trigger: "blur", message: "密码不能为空"}
        ],
        code: [{required: true, trigger: "change", message: "验证码不能为空"}]
      },
      loading: false,
      redirect: undefined
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    },
    loginType: function (val) {
      if (val === "pwd") {
        this.getCode();
        this.getCookie();
      }
    }
  },
  created() {

  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled = res.data.captchaEnabled === undefined ? true : res.data.captchaEnabled;
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.data.img;
          this.loginForm.uuid = res.data.uuid;
        }
      });
    },
    async getLoginQr() {
      const {protocol, host} = window.location;
      const scanHost = await getConfigKey("sys.login.scan.host").then(res => res.msg);
      const scanUrl = await getConfigKey("sys.login.scan.url").then(res => res.msg);
      const scanAppId = await getConfigKey("sys.login.scan.appid").then(res => res.msg);
      const contextPath = process.env.VUE_APP_CONTEXT_PATH;
      //URL编码
      const redirectUrl = encodeURIComponent(`${protocol}//${host}${contextPath}sso_login`);
      window.location.href = `${scanHost}${scanUrl}?response_type=code&appid=${scanAppId}&redirect_uri=${redirectUrl}&scope=snsapi_base`
      //window.location.href = `${location.protocol}//${location.host}/sso_login?code=xx12345`;
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, {expires: 30});
            Cookies.set("password", encrypt(this.loginForm.password), {expires: 30});
            Cookies.set('rememberMe', this.loginForm.rememberMe, {expires: 30});
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove('rememberMe');
          }
          this.$store
              .dispatch("Login", this.loginForm)
              .then(() => {
                this.$router.push({path: this.redirect || "/"});
              })
              .catch(() => {
                this.loading = false;
                this.getCode();
              });
        }
      });
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>

.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-color: #0D1D30;
  position: relative;
}

.main {
  position: relative;
  width: 940px;
  height: 495px;
  background: rgba(25, 41, 59, 1);
  border-radius: 12px;
  box-shadow: 0px 2px 12px 0px rgba(9, 23, 39, 1);
  color: rgba(255, 255, 255, 1);

  .img1 {
    position: absolute;
    top: 46px;
    left: 53px;
    width: 110px;
  }

  .img2 {
    position: absolute;
    top: 63px;
    left: 132px;
    width: 348px;
    height: 299px;
  }

  h3 {
    margin-left: 52px;
    margin-top: 383px;
    margin-bottom: 0;
    font-size: 32px;

  }

  h4 {
    margin-left: 52px;
    font-size: 24px;
    margin-top: 16px;
  }

  .login-form {
    position: absolute;
    right: 0;
    top: 0;
    border-radius: 6px;
    background: #ffffff;
    width: 299px;
    height: 495px;
    padding: 58px 33px 33px 33px;

    .title {
      width: 85px;
      font-size: 20px;
      margin: 0px auto 62px auto;
      border-bottom: 3px rgba(27, 74, 127, 1) ridge;
      text-align: center;
      color: #333333;
      padding-bottom: 8px;
    }


    .el-input {
      height: 32px;

      input:focus {
        color: rgba(6, 104, 185, 1);
      }

      input {
        outline: none;
        height: 38px;
        border-top: 0;
        border-left: 0;
        border-right: 0;
        border-radius: 0;
      }


    }

    .color-class {
      color: rgba(6, 104, 185, 1);
    }

    .input-icon {
      height: 39px;
      width: 19px;
      margin-left: 2px;
    }
  }
}

#qrCodeLogin {
  display: flex;
  flex-direction: column;
  flex-wrap: nowrap;
  align-items: center;
  justify-content: center;
}

.qr-login {
  background-color: rgba(222, 241, 255, 0.71);
  width: 120px;
  height: 120px;
  border-radius: 10%;
  padding: 10px;

  .qr-img {
    width: 100px;
    height: 100px;
  }

}

.change-login-type {
  position: absolute;
  width: 60px;
  height: 40px;
  background-color: rgba(178, 229, 253, 0.85);
  right: 0;
  bottom: 0;
  border-radius: 5px 0 0 0;
  display: flex;
  align-items: center;
  justify-content: center;

  .change-icon {
    font-weight: bold;
    color: black;
    font-size: 28px;
    width: 30px;
    height: 30px;
  }
}


.login-code {
  position: absolute;
  width: 88px;
  height: 32px;
  right: 3px;
  bottom: 4px;

  img {
    width: 88px;
    height: 32px;
    cursor: pointer;
    vertical-align: middle;
  }
}

.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: absolute;
  transform: translateY(273px);
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;

  span {
    img {
      height: 12px;
    }
  }
}


</style>
